<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小西瓜 <751707632@qq.com>
// +----------------------------------------------------------------------
namespace app\api\model;

use app\admin\model\RouteModel;
use think\Model;
use tree\Tree;

class PortalCategoryModel extends Model
{

    protected $type = [
        'more' => 'array',
    ];

    /**
     * 查询分类 
     * @param int $parentId 需要查询分类父级ID
     * @param int $grade 需要查询几级
     * @param string $field 查询字段
     * @returns string
     */
    public function categoryTree($parentId=0, $grade=3, $field="id,parent_id,name,more")
    {
        $list = array();
        // 一级
        $one = $this->where(['parent_id'=>$parentId, 'status'=>1])->field($field)->order('list_order asc, id asc')->select()->toArray();
        $list = $one;

        // 二级
        if ( $grade > 1 ) {
            foreach($one as $k => $v){
                $two = $this ->where(['parent_id'=>$v['id'], 'status'=>1])->field($field)->order('list_order asc, id asc')->select()->toArray();
                // 三级
                if ( $grade > 2 ) {
                    foreach($two as $key => $val){
                        $three = $this ->where(['parent_id'=>$val['id'], 'status'=>1])->field($field)->order('list_order asc, id asc')->select()->toArray();
                        $two[$key]['son'] = $three;
                    }
                } 
                $list[$k]['son'] = $two;
            }
        } 
        return $list;
    }
}